<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PayLog extends Model
{
    /**
     * 支付请求数据
     */
    const PAY_REQUEST = 1;
    /**
     * 异步返回支付结果数据
     */
    const PAY_RESPONSE = 2;
    /**
     * 支付异常数据
     */
    const PAY_EXCEPTION = 3;
    /**
     * 支付结果
     */
    const PAY_RESULT = 4;
    /**
     * 已支付完成过
     */
    const PAID = 5;
    /**
     * 支付没有找到
     */
    const PAY_NO_FOUND = 6;
    /**
     * 支付失败
     */
    const PAY_FAILURE = 7;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['trade_no', 'content', 'log_type'];

    protected $casts = [
        'content' => 'array',
    ];

    public function storeRequest($batch_no, $data)
    {
        return $this->store($batch_no, $data, self::PAY_REQUEST);
    }

    public function storeResponse($batch_no, $data)
    {
        return $this->store($batch_no, $data, self::PAY_RESPONSE);
    }

    public function storeException($batch_no, $data)
    {
        return $this->store($batch_no, $data, self::PAY_EXCEPTION);
    }

    public function storeResult($batch_no, $data)
    {
        return $this->store($batch_no, $data, self::PAY_RESULT);
    }

    public function storePaid($batch_no, $data = [])
    {
        return $this->store($batch_no, $data, self::PAID);
    }

    public function storeNoFound($batch_no, $data)
    {
        return $this->store($batch_no, $data, self::PAY_NO_FOUND);
    }

    public function storeFailure($batch_no, $data = [])
    {
        return $this->store($batch_no, $data, self::PAY_FAILURE);
    }

    private function store($batch_no, $data, $type)
    {
        $model = clone $this;

        return $model->fill([
            'trade_no' => $batch_no,
            'content'  => $data,
            'log_type' => $type,
        ])->save();
    }
}
